﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace System.Drawing
{
    public static class BitmapEx
    {
        public static BitmapImage GetBitmapImage(this Bitmap bmp)
        {
            try
            {
                BitmapImage bitmapImage = new BitmapImage();
                using (MemoryStream memory = new MemoryStream())
                {
                    
                    bmp.Save(memory, ImageFormat.Png);
                    memory.Position = 0;

                    bitmapImage.BeginInit();
                    bitmapImage.StreamSource = memory;
                    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                    bitmapImage.EndInit();
                }
                return bitmapImage;
            }
            catch (Exception ex)
            {
                return null;
            }
            
        }

        public static Bitmap DrawLine(this Bitmap bmp, Point From, Point To)
        {
            Bitmap result = bmp;
            Pen pen = new Pen(System.Drawing.Color.Red, 2.0F);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawLine(pen, From, To);
            }
            return result;
        }

        public static Bitmap GetNonIndexedBitmap(this Bitmap bmp)
        {
            Bitmap result = new Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format32bppArgb);
            using (Graphics g = Graphics.FromImage(result))
            {
                g.DrawImage(bmp, 0, 0);
            }
            return result;
        }
    }
}
